package osmedile.intellij.checkmyvars;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;

/**
 * @author Olivier Smedile
 * @version $Id$
 */
public class AskCreateSampleAction extends CreateSampleAction {

    public static final String ACTION_ID = "osmedile.intellij.checkmyvars.AskCreateSampleAction";


    public void doAction(Project project, DataContext dataContext) {
        boolean runAction = true;
        if (getCmvAppComp().getConfig().getAskToCreateSample()) {
            CreateSampleGUI sampleDialog = new CreateSampleGUI();
            sampleDialog.pack();
            sampleDialog.setModal(true);
            sampleDialog.setLocationRelativeTo(null);
            sampleDialog.setVisible(true);

            if (!sampleDialog.isAskSampleSelected()) {
                getCmvAppComp().getConfig().setAskToCreateSample(false);
            }

            if (!sampleDialog.isOkPressed()) {
                runAction = false;
            }

        }

        if (runAction) {
            super.doAction(project, dataContext);
        }
    }


    private CheckMyVarsAppComp cmvAppComp;

    protected CheckMyVarsAppComp getCmvAppComp() {
        if (cmvAppComp == null) {
            cmvAppComp = ApplicationManager.getApplication().getComponent(CheckMyVarsAppComp.class);
        }
        return cmvAppComp;
    }
}